package com.blocks.controller;

import com.blocks.ScriptBlocksApplication;
import com.blocks.blocks.Block;
import com.blocks.model.BlockSides;
import com.jme3.math.FastMath;
import com.jme3.math.Vector3f;

public class BlockUtils {
	public static void updateHUDBlock() {
		Long hudBlockIndex = ScriptBlocksApplication.getInstance()
				.getHudBlockIndex();
		Block blockInstance = ScriptBlocksApplication.getInstance()
				.getBlockTypeMap().get(hudBlockIndex);
		Block pickBlock;
		
		try {
			String scriptName = blockInstance.getScriptName();
			
			pickBlock = blockInstance.getClass().newInstance();	
			pickBlock.setScriptName(scriptName);
			pickBlock.init();
			ScriptBlocksApplication.getInstance().setupPickBlock(pickBlock);
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	
	public static Vector3f calculatePivot(BlockSides blockSides) {
		if (BlockSides.SIDE_ABOVE == blockSides) {
			return new Vector3f(0.0f, 2.0f, 0.0f);
		}
		if (BlockSides.SIDE_BELOW == blockSides) {
			return new Vector3f(0.0f, -2.0f, 0.0f);
		}
		if (BlockSides.SIDE_FRONT == blockSides) {
			return new Vector3f(0.0f, 0.0f, -2.0f);
		}
		if (BlockSides.SIDE_BACK == blockSides) {
			return new Vector3f(0.0f, 0.0f, 2.0f);
		}
		if (BlockSides.SIDE_RIGHT == blockSides) {
			return new Vector3f(-2.0f, 0.0f, 0.0f);
		}
		if (BlockSides.SIDE_LEFT == blockSides) {
			return new Vector3f(2.0f, 0.0f, 0.0f);
		}
		return Vector3f.ZERO;
	}

	public static Vector3f calculateRotation(BlockSides blockSides1,
			BlockSides blockSides2) {
		float yaw = 0.0f; // X Radians(PI/2) = 90 degrees;
		float roll = 0.0f;
		float pitch = 0.0f;// Z
		if (BlockSides.SIDE_ABOVE == blockSides2) {
			roll = FastMath.HALF_PI; // Y Radians(PI/2) = 90 degrees;
		}
		if (BlockSides.SIDE_BELOW == blockSides2) {
			roll = -FastMath.HALF_PI; // Y Radians(PI/2) = 90 degrees;
		}
		if (BlockSides.SIDE_FRONT == blockSides2) {
			yaw = FastMath.HALF_PI; // Y Radians(PI/2) = 90 degrees;
		}
		if (BlockSides.SIDE_BACK == blockSides2) {
			yaw = -FastMath.HALF_PI; // Y Radians(PI/2) = 90 degrees;
		}
		if (BlockSides.SIDE_RIGHT == blockSides2) {
			pitch = FastMath.HALF_PI; // Y Radians(PI/2) = 90 degrees;
		}
		if (BlockSides.SIDE_LEFT == blockSides2) {
			pitch = -FastMath.HALF_PI; // Y Radians(PI/2) = 90 degrees;
		}
		return new Vector3f(yaw, roll, pitch);
	}
}
